package br.com.fatecpg.operon.frames;
import br.com.fatecpg.operon.Conexao;
import java.sql.*;
import javax.swing.*;

public class DisponibilidadeHorarioProfessoresIFrame extends javax.swing.JInternalFrame {


    /** Creates new form DisponibilidadeHorarioProfessoresIFrame */
    public DisponibilidadeHorarioProfessoresIFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();

        jCheckBox1.setText("jCheckBox1");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Professor");

        jTextField1.setName("txtNome"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("DISPONIBILIDADE DE HORÁRIO DOS PROFESSORES");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Horários disponíveis");

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Gravar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton2.setText("Alterar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton3.setText("Excluir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton4.setText("Pesquisar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jComboBox1.setName("cbHorarios"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addComponent(jLabel2))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addComponent(jButton1)
                .addGap(7, 7, 7)
                .addComponent(jButton2)
                .addGap(5, 5, 5)
                .addComponent(jButton3)
                .addGap(9, 9, 9)
                .addComponent(jButton4))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel2)
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(118, 118, 118)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4)))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-495)/2, (screenSize.height-391)/2, 495, 391);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Conexao c = new Conexao();

         try
         {
            String sqlInsert  = "INSERT INTO PROFESSORES(NOME)VALUES("+jTextField1.getText()+")";
            Conexao.con.createStatement().execute(sqlInsert);
        	}
            catch (Exception ex)
            {
               ex.printStackTrace();
               try
               {
                  Conexao.con.rollback();
               }
                  catch (SQLException e1)
                  {
                     System.out.print(e1.getStackTrace());
                  }
            }
         

         try
         {
            String sqlInsertHorario  = "INSERT INTO HORARIOS_DE_AULA(HORARIO)VALUES("+jComboBox1.getSelectedItem()+")";
            Conexao.con.createStatement().execute(sqlInsertHorario);
        	}
            catch (Exception ex)
            {
               ex.printStackTrace();
               try
               {
                  Conexao.con.rollback();
               }
                  catch (SQLException e1)
                  {
                     System.out.print(e1.getStackTrace());
                  }
            }
         
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

         Connection conn = null;
         String sqlSelect = "Update Professores Set NOME = "+jTextField1.getText()+"";

         PreparedStatement stm = null;
         ResultSet rs = null;

         int codigo = Integer.parseInt(JOptionPane.showInputDialog("Digite o código do professor\n para alteração das informações:"));

         try
         {
            stm = conn.prepareStatement(sqlSelect);

         }
            catch (Exception e)
            {
               e.printStackTrace();
               try
               {
                  conn.rollback();
               }
                  catch (SQLException e1)
                  {
                     System.out.print(e1.getStackTrace());
                  }
            }
         finally
         {
            if (stm != null)
            {
               try
               {
                  stm.close();
               }
                  catch (SQLException e1)
                  {
                     System.out.print(e1.getStackTrace());
                  }
            }
         }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String sqlSelect = "SELECT NOME,HORARIO FROM PROFESSORES WHERE NOME = ? INNER JOIN( CURSOS.CURSO_ID = HORARIOS_DE_AULA.HORARIO_ID)";


         PreparedStatement stm = null;
         ResultSet rs = null;
         Connection conn = null;
         String nome = JOptionPane.showInputDialog("DIGITE O NOME \n DO PROFESSOR:");


         try
         {
            stm = conn.prepareStatement(sqlSelect);
            rs = stm.executeQuery();

            if (rs.next())
            {
               JOptionPane.showMessageDialog(null,"\n Nome: "+rs.getString(2));
            }
         }



            catch (Exception e)
            {
               e.printStackTrace();
               try
               {
                  conn.rollback();
               }
                  catch (SQLException e1)
                  {
                     System.out.print(e1.getStackTrace());
                  }
            }
         finally
         {
            if (stm != null)
            {
               try
               {
                  stm.close();
               }
                  catch (SQLException e1)
                  {
                     System.out.print(e1.getStackTrace());
                  }
            }
         }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String sqlDelete = "DELETE FROM PROFESSORES WHERE PROF_ID = ?";
        Connection conn = null;
        PreparedStatement stm = null;
         try
         {

            stm = conn.prepareStatement(sqlDelete);
            stm.execute();
         }
            catch (Exception e)
            {
               e.printStackTrace();
               try
               {
                  conn.rollback();
               }
                  catch (SQLException e1)
                  {
                     System.out.print(e1.getStackTrace());
                  }
            }
         

    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables


}


